<?php


namespace app\index\controller;
use think\Controller;
use think\Db;

class Kf extends Controller
{

    public function task(){
        $h = date('H');
        $n=60*60*1000; //刷新时间 1小时刷新一次页面
        print_r('<div style="text-align: center;font-size:30px;line-height: 2">外包扣费，一秒扣一条<br>'.date('Y-m-d H:i:s',time()).'：<br>');


//        更新redis
        if($h>=21){
            $this->test();
//            echo "扣费结束";
        }else{
            echo "不在时间段";
        }

        print_r('</div>');
        echo ("<script>setTimeout('window.location.reload()',$n)</script>");//            自动刷新
    }



    public function reset(){

//        每天重置 排名，扣费状态
        Db::table('tb_webkey')->where(['state'=>1])->update(['zPm'=>Db::raw('nowPm'), 'nowPm'=>0, 'iskf'=>0]);

//        重置当前封顶金额
        Db::table('tb_webinfo')->where(['state'=>1,'istop'=>1])->update(['nowtop'=>0]);


    }


    public function test()
    {
//        $userid=input('userid');
//        当前时间
        $intime=date("Y-m-d H:i:s",time());

        $rows=Db::table('tb_webkey k')
            ->field('u.money userMoney,u.qftime uqf,w.qftime wqf,w.istop wistop,w.topMoney,w.nowtop,k.*')
            ->join('tb_user u','u.id=k.userid')
            ->join('tb_webinfo w','w.id= k.webid')
            ->where([
                'k.state'=>1,
                'k.iskf'=>0,
//                'u.id'=>$userid,
                'k.nowpm'=>['gt',0],
            ])
            ->whereTime('pmtime', 'today')
            ->limit(1)
            ->order('id')
            ->select();


        $total_money=0;
        if($rows) {
            foreach ($rows as $k => $v) {
                $money = 0;      //金额
                $qfstate = 0; //欠费状态
                if ($v['typeid'] > 0) {
                    if ($v['nowPm'] > 0 && $v['nowPm'] <= 10) {
                        $money = $v['money_one'];
                    } elseif ($v['nowPm'] > 10 && $v['nowPm'] <= 20) {
                        $money = $v['money_two'];
                    }
                    if ($v['qftime'] <> null && $v['qftime'] <> "") {
                        $qfstate = 1;
                    }

//                如果到了封顶金额就暂停
                    if ($v['wistop'] == 1) {
                        $webinfo = Db::table('tb_webinfo')->field('topMoney,nowtop')->where(['id' => $v['webID'],])->find();
                        if ($webinfo['nowtop'] + $money > $v['topMoney']) {
                            $money = 0;
                        }
                    }

                    if ($money > 0) {
                        $total_money += $money;
                        if ($v['istop'] == 1) {
//                        如果封顶，更新当前封顶金额
                            Db::table('tb_webinfo')->where(['id' => $v['webID'], 'istop' => 1])->setInc('nowtop', $money);
                        }

//////                更新该词 消费金额，扣费状态更改
                        Db::table('tb_webkey')->where(['id' => $v['id']])->update(['moneypay' => Db::raw('moneypay+' . $money), 'iskf' => 1,]);
//
//////              插入tb_money 表
                        Db::table('tb_money')->insert(['userID' => $v['userid'], 'webid' => $v['webID'], 'keyid' => $v['id'], 'seoid' => $v['seoid'], 'keypm' => $v['nowPm'], 'typeid' => $v['typeid'], 'Name' => 'seo扣费' . $v['name'], 'money' => $money, 'qfstate' => $qfstate, 'intime' => $intime,]);

//                    更新用户余额，消费金额
                        Db::table('tb_user')->where('id', $v['userid'])->update([
                            'moneypay' => Db::raw('moneypay+' . $money),
                            'money' => Db::raw('money-' . $money)
                        ]);
//////
//                    更新欠费的用户时间
                        if ($qfstate == 0) {
                            if ($v['userMoney'] - $money < 0) {
                                Db::table('tb_user')->where(['id' => $v['userid'], 'money' => ['lt', 0], 'qftime' => null,])->update(['qftime' => $intime]);
                                Db::table('tb_webinfo')->where(['userid' => $v['userid']])->update(['qftime' => $intime]);
                                Db::table('tb_webkey')->where(['userid' => $v['userid']])->update(['qftime' => $intime]);
                            }
//                    更新优化师钱包
                            Db::table('tb_admin')->where(['id' => $v['seoid']])->setInc('M_money', $money);
                        }

                        print($v['id'] . ':扣费' . $money . '----欠费状态' . $qfstate . '----<br>');
                    } else {
                        Db::table('tb_webkey')->where(['id' => $v['id']])->update(['iskf' => 1]);
                        print($v['id'] . ':不扣费<br>');
                    }
                }
            }
            echo ("<script>setTimeout('window.location.reload()',700);</script>");
        }else{
            print('完成扣费');
        }

//        print($total_money.'<br>');
//        print('完成扣费');
    }


}